import { Component, OnInit, Input, OnChanges } from "@angular/core";
import { OneMapService } from "../one-map.service";
import { DatePipe } from "@angular/common";
import { forkJoin } from "rxjs";
import { NzMessageService } from "ng-zorro-antd";
import { DialogBase } from "../dialog-base";

@Component({
  selector: "app-dialog-pump",
  templateUrl: "./dialog-pump.component.html",
  styleUrls: [
    "../dialog-dam/dialog-dam.component.scss",
    "./dialog-pump.component.scss"
  ]
})
export class DialogPumpComponent extends DialogBase implements OnChanges {
  @Input() data: any;
  tabs: any[] = ["机组状态", "过程线", "旬月特征", "历史同期"];
  activeIdx: number = 0;
  pumpStatus: any = {};

  constructor(
    private mapService: OneMapService,
    public datePipe: DatePipe,
    private message: NzMessageService
  ) {
    super(datePipe);
  }

  ngOnChanges() {
    // TODO
    this.data.code = this.data.code || "YH-18-GS";
    console.log(this.data);
    this.initData();
  }

  initData() {
    switch (this.activeIdx) {
      case 0:
        this.onQueryStatus();
        break;
      case 1:
        this.onQueryProcessList();
        break;
      case 2:
        this.onQueryMonthList();
        break;
      case 3:
        this.onQueryHistory();
        break;
      case 4:
        break;
    }
  }

  onSelectChange(e) {
    this.activeIdx = e.index;
    this.initData();
  }

  getProcessListPump() {
    return this.mapService.getProcessListPump({
      stadiaCode: this.data.code,
      beginTime: this.datePipe.transform(this.beginDate, "yyyy-MM-dd"),
      endTime: this.datePipe.transform(this.endDate, "yyyy-MM-dd"),
      pageSize: 1000
    });
  }

  getMonthListPump() {
    return this.mapService.getMonthListPump({
      stadiaCode: this.data.code,
      year: this.curYear,
      history: `${this.year1.getFullYear()},${this.year2.getFullYear()}`
    });
  }

  onQueryStatus() {
    this.getProcessListPump().subscribe(res => {
      if (res["list"].length) {
        this.pumpStatus = res["list"][res["list"].length - 1];
      }
    });
  }

  onQueryProcessList() {
    this.getProcessListPump().subscribe(res => {
      const list = res["list"];
      this.processOptChange = {
        xAxis: [
          {
            data: this.getItemValues(list, "meaTime", true)
          }
        ],
        series: [
          {
            data: this.getItemValues(list, "fpdLevel")
          },
          {
            data: this.getItemValues(list, "bpdLevel")
          },
          {
            data: this.getItemValues(list, "flux")
          }
        ]
      };
      console.log(this.processOpt);
    });
  }

  onQueryMonthList() {
    this.getMonthListPump().subscribe((data: any) => {
      // console.log(data);
      this.monthData = data;
      this.changeMonthChart();
    });
  }

  onQueryHistory() {
    const history1 = this.mapService.getProcessListPump({
      ...this.getDateList(this.historyYear1),
      stadiaCode: this.data.code
    });
    const history2 = this.mapService.getProcessListPump({
      ...this.getDateList(this.historyYear2),
      stadiaCode: this.data.code
    });
    forkJoin(history1, history2).subscribe(datas => {
      this.historyLists = datas.map(data => {
        return data["list"];
      });
      this.changeHistoryChart();
    });
  }

  changeMonthChart() {
    console.log(this.selectedIdx);
    const data = this.monthData;
    const keys: any[] = Object.keys(data);
    this.monthOptChange = {
      legend: {
        data: keys.map(k => {
          return k == "historyYear" ? "历史年段" : k;
        })
      },
      series: keys.map(key => {
        return {
          name: key == "historyYear" ? "历史年段" : key,
          type: "bar",
          data: data[key].map(item => {
            return item[this.eigenKeys[this.selectedIdx]];
          })
        };
      })
    };
    // console.log(this.monthOptChange);
  }

  changeHistoryChart() {
    const lists = this.historyLists;
    if (!lists[0].length || !lists[1].length) {
      this.message.error("---无数据---");
      return;
    }
    const xAxisData = lists[0].map(item => {
      return item.meaTime.substr(5);
    });
    const legendData: any[] = [];
    const seriesData: any[] = [];
    lists.map(list => {
      const year = list[0].meaTime.substr(0, 4);
      legendData.push(year);
      seriesData.push({
        name: year,
        type: "line",
        data: list.map(item => {
          return item[this.historyKeys[this.hType]];
        })
      });
    });
    this.historyOptChange = {
      legend: {
        data: legendData
      },
      xAxis: {
        data: xAxisData
      },
      series: seriesData
    };
    // console.log(this.historyOptChange);
  }

  // getItemValues(list, key) {
  //     return list.map(i => {
  //         return i[key] || 0;
  //     });
  // }

  // getDateList(year) {
  //     year = year.getFullYear();
  //     const begin = this.datePipe.transform(this.historyBegin, "MM-dd");
  //     const end = this.datePipe.transform(this.historyEnd, "MM-dd");
  //     return {
  //         beginTime: `${year}-${begin}`,
  //         endTime: `${year}-${end}`
  //     };
  // }

  // checkHistoryDate() {
  //     if (
  //         this.getMonthTime(this.historyBegin) >=
  //         this.getMonthTime(this.historyEnd)
  //     ) {
  //         return false;
  //     }
  //     return true;
  // }

  // getMonthTime(date) {
  //     const month = date.getMonth();
  //     const day = date.getDate();
  //     return new Date(1970, month, day).getTime();
  // }
}
